package com.geez.bbl.projector;

import com.geez.bbl.db.type.BblBook;
import com.geez.bbl.db.type.BblChapter;
import com.geez.bbl.db.type.Builder;
import com.geez.bbl.db.type.Verse;
import com.geez.bbl.util.FileUtils;
import com.geez.bbl.util.ProjectResources;

import java.io.File;
import java.util.List;

/**
 * @author biniam.gebremichael
 * @since 6/7/12
 */
public class GenerateZaphina {

    static String prefix = "<XMLBIBLE biblename=\"Tigrigna\">\n";
    static String postfix = "</XMLBIBLE>\n";
    private static File resourceFile = ProjectResources.getResourceFile("TI_ER_CS_ZION.xml");
    //     "<BIBLEBOOK bnumber=\"1\" bname=\"Genesis\">"


    public static void main(String[] args) throws Exception {
        List<BblBook> books = new Builder("ti").build();
        if (args.length > 0) {
            resourceFile = new File(args[0]);
        }
        new GenerateZaphina().createZaphina(books, resourceFile);
        System.out.println("zaphina xml written to " + resourceFile.getAbsolutePath());
    }

    private String chapterAttributes(int no) {
        StringBuilder builder = new StringBuilder();
        builder.append("\t\t<CHAPTER cnumber=\"").append(no).append("\">\n");
        return builder.toString();
    }

    private String bookAttributes(int bookNo, String longName, String shortName) {
        StringBuilder builder = new StringBuilder();
        builder.append("\t<BIBLEBOOK bnumber=\"")
                .append(bookNo)
                .append("\" bname=\"")
                .append(longName)
                .append("\" bsname=\"")
                .append(shortName)
                .append("\">\n");
        return builder.toString();
    }

    public void createZaphina(List<BblBook> books, File resourceFile) {
        StringBuilder builder0 = new StringBuilder();
        builder0.append(prefix);
        for (BblBook book : books) {
            builder0.append(getBook(book));
        }
        builder0.append(postfix);
        FileUtils.save(resourceFile, builder0.toString());

    }

    private String getBook(BblBook book) {
        StringBuilder builder = new StringBuilder();
        builder.append(bookAttributes(book.getBook_no(), book.getLong_name(), book.getShort_name()));
        for (BblChapter chapter : book.getChapters().values()) {
            builder.append(getChapter(chapter));

        }

        builder.append("\t</BIBLEBOOK>\n");
        return builder.toString();
    }

    private String getChapter(BblChapter chapter) {
        StringBuilder builder = new StringBuilder();
        builder.append(chapterAttributes(chapter.getId()));
        for (Verse verse : chapter.getVerses()) {
            builder.append(getVerses(verse));
        }
        builder.append("\t\t</CHAPTER>\n");
        return builder.toString();
    }

    private static String getVerses(Verse verse) {
        StringBuilder builder = new StringBuilder();
        for (int j = verse.getStartId(); j <= verse.getEndId(); j++) {
            builder.append("\t\t\t<VERS vnumber=\"")
                    .append(j)
                    .append("\">");
            builder.append(verse.getPlainValue().trim().replaceAll("\\s+", " "));
            builder.append("</VERS>\n");
        }
        return builder.toString();
    }


}
